<?php

namespace App\Repository\Commit;

use App\Models\Config as model;
use App\Repository\BaseRepository;

class ConfigDataRepository extends  BaseRepository
{
  public function __construct(model $model)
  {
    $this->model = $model;
    parent::__construct();
  }
  public function index($request)
    {
        $where['like'] = ['order_nums'];
        $cron['like'] = $this->getWhere($where['like'], $request);
        if (!empty($request['created_start']) && !empty($request['created_end'])) {
            $cron['between']['create_time'][] = $request['created_start'];
            $cron['between']['create_time'][] = $request['created_end'];
        }

        $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
        $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
        $pageNums = 0;
        $cron['limit']['number'] =isset($request['pagenums'])?($request['page_size']):10;
        if(isset($request['page']) && !empty(($request['page']))){
            $pageNums =($request['page']) * $cron['limit']['number'];
        }
        $cron['limit']['start'] =$pageNums;

        return $this->search($this->model, $cron);

    }

  public function store($request)
  {
      // if($this->model->where(['order_nums'=>$request->get('order_nums')])->first()){
      //     return $this->result(-1,'该数据已添加');
      // }
      if(!empty($request->user->id)){
          $request->offsetSet('users_id',$request->user->id);
      }
      if(!empty($request->user->company_master_id)){
          $request->offsetSet('company_master_id',$request->user->company_master_id);
      }
      $request->offsetSet('create_time', date('Y-m-d H:i:s'));
      // if(empty($request->get('order_num'))){
      //     $request->offsetSet('order_num', );
      // }
      $res = $this->model->create($request->all());
      return $this->returnMsg(0, '添加成功', $res);
  }

  public function save($request, $id)
  {
      if(!empty($request->get('name'))){
          $count = $this->model->where(['name'=>$request->get('name')])->count();
          if($count >1){
              return $this->returnMsg(-1,'该数据已添加');
          }
      }
      $userRes = $this->model->where('order_num',$id)->first();
      if (!$userRes) {
          //修改订单的状态
          $this->orderModel->where('order_num',$id)->update(['state'=>3]);

          return $this->store($request);
      }
      foreach ($this->model->getFillable() as $v) {
          if (isset($request[$v]) && $request[$v] != '') {
              $userRes->$v = $request[$v];
          }
      }
      $userRes->update_time= date('Y-m-d H:i:s');
      $userRes->save();
      $this->orderModel->where('order_num',$id)->update(['state'=>3]);
      return $this->returnMsg(0, '修改成功', true);
  }

  public function show($id)
  {
      $res = $this->model->where('order_num', $id)->first();
      return $this->returnMsg(0, '获取成功', $res);
  }

  public function destroy($id)
  {
      $this->model->destroy($id);
      return $this->returnMsg(0, '删除成功', true);
  }
  public function oneSel($type){
    $where = [
      'config_key' => $type,
    ];
    $resData = $this->model->where($where)->select(['config_value as value'])->get();
    $returnData = [];
    if(!empty($resData)){
      $returnData = usfirstArr($resData);
    }
    return $this->returnMsg(200,'查询成功',$returnData);
  }
}
